import Taro, { useRouter } from '@tarojs/taro';
// const BASE_URL = 'http://192.168.130.48:8888'
const BASE_URL = 'https://cxj.szzkba.cn/prod-api'
export function request(method = 'GET', url, data, token) {
    return new Promise(function (resolve, reject) {
        Taro.request({
            url: BASE_URL + url,
            data: data,
            method: method,
            header: {
                'content-type': 'application/json',
                'Authorization': token || 'Bearer ' + Taro.getStorageSync('token')
                // 'Authorization': 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjM2YTVmNmNjLTMzZmEtNGRhNC1hYjdiLTU4OWM4ZGVlNjZjOSJ9.DGes8VTeb_k4ZJ3A6bpmOaxv7U2vABo0Vk-5RSEqfmHBdj9QyyKEqSx9XNdHyOPGqXFzvbX4SjmtnkXyy9KeXA'
            },
            success: function (res) {
                if (res.data.code == 200) {
                    resolve(res.data);
                } else if (res.data.code == 401) {
                    const router = useRouter();
                    Taro.showModal({
                        title: '请登录',
                        content: '登录后体验更好的服务',
                        showCancel: false,
                        success: function (res) {
                            Taro.reLaunch({
                                url: '/pages/mypage/index?name=myPage'
                            })
                        }
                    })
                    Taro.removeStorage({
                        key: 'token',
                        success: function (res) {
                            //console.log(res)
                        }
                    })
                    Taro.removeStorage({
                        key: 'user',
                        success: function (res) {
                            //console.log(res)
                        }
                    })
                    reject("登录已过期");
                } else {
                    resolve(res.data)
                }
            },
            fail: function (err) {
                //console.log(err)
                reject("服务器连接异常，请检查网络");
            },
            complete: function () {
            }
        })
    })
}

